import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Eatyet } from '../entity/po/eatyet.po';
import { Repository } from 'typeorm';

@Injectable()
export class EatyetService {
  constructor(
    @InjectRepository(Eatyet)
    private readonly eatyetRepository: Repository<Eatyet>,
  ) {}

  async random() {
    let sum = await this.eatyetRepository.count();
    let offset = this.randomInt(0, sum);
    let promise = await this.eatyetRepository.find({
      skip: offset,
      take: 1,
    });
    if (promise && promise.length > 0) {
      return promise[0];
    }
    return [];
  }

  randomInt(min: number, max: number): number {
    return Math.floor(Math.random() * (max - min + 1)) + min;
  }
}
